package com.jsh.frame.myframe2.utils;

import android.content.Context;
import android.support.annotation.NonNull;
import android.widget.PopupWindow;

import com.bigkoo.alertview.AlertView;
import com.bigkoo.alertview.OnDismissListener;
import com.bigkoo.alertview.OnItemClickListener;
import com.bigkoo.svprogresshud.SVProgressHUD;


/**
 * Created by zhangyang on 2016/8/11.
 * 弹窗工具
 */
public class DialogUtil {
    public static Context context;
    public static String title;
    public static String[] options;
    public static OnItemClickListener listener;

    private DialogUtil(Builder builder) {
        this.context = builder.context;
        this.title = builder.title;
        this.options = builder.options;
        this.listener = builder.listener;

    }

    public static class Builder {
        private Context context;
        private String title;
        private String[] options;
        private OnItemClickListener listener;

        public Builder with(@NonNull Context context) {
            this.context = context;
            return this;

        }

        public Builder setTitle(String titlel) {
            this.title = titlel;
            return this;
        }

        public Builder setOtions(String[] options) {
            this.options = options;
            return this;
        }

        public Builder setOnButtonClickListener(OnItemClickListener listener) {
            this.listener = listener;
            return this;
        }

        public DialogUtil create() {
            return new DialogUtil(this);
        }

    }

    public static void show() {
        alertView = new AlertView(title, null, "取消", null, options, context, AlertView.Style.ActionSheet, listener);
        alertView.setOnDismissListener(dissMissListener);
        alertView.setCancelable(true).show();
        isAlertViewshowing = true;
    }

    private static AlertView alertView;

    private static MyDissMissListener dissMissListener = new MyDissMissListener();

    private static boolean isAlertViewshowing;
    public static SVProgressHUD svProgressHUD;

    /**
     * 显示actionsheet
     *
     * @param title
     * @param options
     * @param context
     * @param listener
     */
    public static void showActionSheet(String title, String[] options, Context context, OnItemClickListener listener) {
        alertView = new AlertView(title, null, "取消", null, options, context, AlertView.Style.ActionSheet, listener);
        alertView.setOnDismissListener(dissMissListener);
        alertView.setCancelable(true).show();
        isAlertViewshowing = true;
    }

    public static void showActionSheet(String title, String content, String[] options, Context context, OnItemClickListener listener) {
        alertView = new AlertView(title, content, "取消", null, options, context, AlertView.Style.ActionSheet, listener);
        alertView.setOnDismissListener(dissMissListener);
        alertView.setCancelable(true).show();
        isAlertViewshowing = true;
    }

    public static void showDiaLog(String title, String msg, String[] options, Context context, OnItemClickListener listener) {
//        alertView = new AlertView(title, msg, null, null, options, context, AlertView.Style.Alert, listener);
//        alertView.setOnDismissListener(dissMissListener);
//        alertView.setCancelable(false).show();
//        isAlertViewshowing = true;
        showDiaLog(title, msg, options, context, true, listener);
    }

    public static void showDiaLog(String title, String msg, String[] options, Context context, boolean cancelable, OnItemClickListener listener) {
        alertView = new AlertView(title, msg, null, null, options, context, AlertView.Style.Alert, listener);
        alertView.setOnDismissListener(dissMissListener);
        alertView.setCancelable(cancelable).show();
        isAlertViewshowing = true;
    }

    public static PopupWindow popupWindow;


    /**
     * 弹窗消失
     */
    public static void dissMiss() {
        if (alertView != null) {
            alertView.dismiss();
        }
    }

    public static boolean isShowingAlertView() {
        return isAlertViewshowing;
    }

    static class MyDissMissListener implements OnDismissListener {
        @Override
        public void onDismiss(Object o) {
            isAlertViewshowing = false;
        }
    }

    public boolean getProgressStatus() {
        return svProgressHUD.isShowing();
    }

    protected void showLabel() {
        svProgressHUD.show();
    }

    public static void showLoadingLabel(Context context) {
        showLoadingLabel(context, "加载中...");
    }

    public static void showLoadingLabel(Context context, String msg) {
        svProgressHUD = new SVProgressHUD(context);
        svProgressHUD.showWithStatus(msg);
    }

    public static void dismissLoadingLabel() {
        if (svProgressHUD.isShowing()) {
            svProgressHUD.dismiss();
        }
    }
}
